package com.leetcode.程序员面试金典.medium;

/**
 * @author 覃国强
 * @date 2021-04-08 23:33
 */
public class 面试题_17_09_第k个数 {

  public static void main(String[] args) {
    Solution solution = new Solution();
    System.out.println(solution.getKthMagicNumber(7));
  }


  static
  class Solution {
    public int getKthMagicNumber(int k) {
      int[] nums = new int[k];
      nums[0] = 1;
      int p3 = 0;
      int p5 = 0;
      int p7 = 0;
      for (int i = 1; i < k; ++i) {
        nums[i] = Math.min(nums[p3] * 3, Math.min(nums[p5] * 5, nums[p7] * 7));
        // 这里用多个 if，而不是 if else 是因为 nums[p3] * 3 可能等于 nums[p5] * 5，这时两个指针都要往前走
        if (nums[i] == nums[p3] * 3) {
          ++p3;
        }
        if (nums[i] == nums[p5] * 5) {
          ++p5;
        }
        if(nums[i] == nums[p7] * 7) {
          ++p7;
        }
      }
      return nums[k - 1];
    }
  }

}
